package com.socialbank.web.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.socialbank.web.FinanceBean.BankCertificateDeposits;
import com.socialbank.web.bean.ResultMessage;
import com.socialbank.web.mapper.BankCertificateDepositsMapper;
import com.socialbank.web.mapper.RecommendGoodsMapper;
import com.socialbank.web.service.BankCertificateDepositsService;
import com.wqdata.auth.util.ExcelUtils;
import com.wqdata.auth.util.UserUtils;
@Service
public class BankCertificateDepositsServiceImpl implements BankCertificateDepositsService{

	@Autowired
	private SqlSession sqlSession;
	
	@Override
	public Object save(MultipartFile multiparFile) {
		// TODO Auto-generated method stub
		BankCertificateDepositsMapper mapper = sqlSession.getMapper(BankCertificateDepositsMapper.class);
		//从excel表导入数据
		int count=0;
		if(!multiparFile.isEmpty()){
			CommonsMultipartFile cf= (CommonsMultipartFile)multiparFile;
			DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
			File newFile = fi.getStoreLocation();
			List<String> list = new ArrayList<>();
			list.add("startMoney");
			list.add("deadline");
			list.add("baseRate");
			list.add("strikeRate");
			list.add("buoyancyRatio");
			ExcelUtils.setColumns(list);
			List<BankCertificateDeposits> BankCertificateDeposits = ExcelUtils.readExcel(newFile,BankCertificateDeposits.class);
			String deptId=String.valueOf(UserUtils.getCurrrentDeptId());
			Date date=new Date();
			for(int i=0;i<BankCertificateDeposits.size();i++){
				BankCertificateDeposits.get(i).setDeptId(deptId);
				BankCertificateDeposits.get(i).setCreateDate(date);
			}
			count=mapper.save(BankCertificateDeposits);
		}
		return new ResultMessage(888,"成功插入"+count+"条信息",null);
	}

	@Override
	public Object del(String deptId) {
		// TODO Auto-generated method stub
		BankCertificateDepositsMapper mapper = sqlSession.getMapper(BankCertificateDepositsMapper.class);
		mapper.del(deptId);
		return new ResultMessage(888, "删除成功", null);
	}

	@Override
	public List<BankCertificateDeposits> getList(String deptId) {
		// TODO Auto-generated method stub
		BankCertificateDepositsMapper mapper = sqlSession.getMapper(BankCertificateDepositsMapper.class);
		List<BankCertificateDeposits> list=mapper.getList(deptId);
		BankCertificateDeposits certificateDeposit=new BankCertificateDeposits();
		for(int i=0;i<list.size();i++){
			certificateDeposit=list.get(i);
			int deaLine=Integer.valueOf(certificateDeposit.getDeadline());
			if(deaLine==6){
				certificateDeposit.setDeadline("半年");
			}else if(deaLine%12==0){
				certificateDeposit.setDeadline(deaLine/12+"年");
			}else{
				certificateDeposit.setDeadline(deaLine+"个月");
			}
		}
		return list;
	}


}
